package com.youlu.campus.web.community.controller;

import com.youlu.campus.entity.VO.req.CategoryQueryVO;
import com.youlu.campus.entity.VO.req.QaOrderReq;
import com.youlu.campus.service.activity.WxMiniContestActivityService;
import com.youlu.campus.service.category.CategoryInfoService;
import com.youlu.campus.service.order.ContestQaOrderService;
import com.youlu.campus.service.qa.impl.UserQaRankingService;
import com.youlu.campus.web.common.base.RequestContext;
import com.youlu.campus.web.community.service.WxMiniContestActivitySignUpService;
import com.youlu.campus.web.common.utils.CommonUtils;
import com.yuelin.infrastructure.quantum.common.QResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 微信小程序活动竞赛控制器
 */
@RestController
public class WxMiniContestController {

    @Autowired
    private WxMiniContestActivityService wxMiniContestActivityService;

    @Autowired
    private WxMiniContestActivitySignUpService wxMiniContestActivitySignUpService;

    @Autowired
    private CategoryInfoService categoryInfoService;

    @Autowired
    private UserQaRankingService userQaRankingService;

    @Autowired
    private ContestQaOrderService contestQaOrderService;

    @Value("${sys.pay.wx.payWechatNotifyUrl}")
    private String notifyUrl;
    /**
     * 获取活动详情-匿名
     *
     *
     * @param activityId
     * @return
     */
    @GetMapping("/public/mini-contest/activity-detail")
    public QResult activityDetail(@RequestParam("activityId") String activityId) {
        return QResult.success(wxMiniContestActivityService.findDetailByActivityId(activityId, RequestContext.getCurUser()));
    }

    @GetMapping("/mini-app/mini-contest/activity-detail")
    public QResult detail(@RequestParam("activityId") String activityId) {
        return QResult.success(wxMiniContestActivityService.findDetailByActivityId(activityId, RequestContext.getCurUser()));
    }

    /**
     * <p>校验报名接口</p>
     * <p>如果存在参与原有活动，则创建报名数据</p>
     * <p>如果不存在，则添加报名数据</p>
     * @param activityId
     * @param activityChannelId 活动渠道ID
     * @return
     */
    @GetMapping("/mini-app/mini-contest/check/sign-up")
    public QResult checkSignUp(@RequestParam("activityId") String activityId,
                               @RequestParam(required = false) String activityChannelId) {
        return QResult.success(wxMiniContestActivitySignUpService.findJoinedSignUpByUserIdAndActivityId(RequestContext.getCurUser(),activityId,activityChannelId));
    }

    @GetMapping("/mini-app/mini/invite-detail")
    public QResult userInviteDetail(@RequestParam("activityId") String activityId){
        return QResult.success(wxMiniContestActivityService.findByActivityId(RequestContext.getCurUser(),activityId));
    }

    @PostMapping("/public/mini/category")
    public QResult categoryList(@RequestBody CategoryQueryVO categoryQuery){
        categoryQuery.setPlatform(RequestContext.getCurPlatform());
        return QResult.success(categoryInfoService.list(categoryQuery));
    }

    /**
     * 查询分数排行榜
     *
     * @param activityId
     * @param qaInfoId
     * @return
     */
    @GetMapping("/mini-app/contest/qa-ranking")
    public QResult gradeRanking(String activityId, String qaInfoId,
                                @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize) {
        String platform = RequestContext.getCurPlatform();
        return QResult.success(userQaRankingService.findQaScoreRankingByActivityId(activityId,qaInfoId,pageNo,pageSize,platform));
    }


    /**
     * 竞赛答题订单支付接口
     * @param request
     * @param bindingResult
     * @param httpRequest
     * @return
     */
    @PostMapping("/mini-app/contest/qa-order/pay")
    public QResult qaOrderPay(@RequestBody @Validated QaOrderReq request, BindingResult bindingResult, HttpServletRequest httpRequest){
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        request.setAppId(RequestContext.getAppId());
        request.setPlatform(RequestContext.getCurPlatform());
        request.setCurrentUser(RequestContext.getCurUser());
        request.setClientIp(CommonUtils.getRemoteIp(httpRequest));
        request.setNotifyUrl(notifyUrl);
        return QResult.success(contestQaOrderService.createOrder(request));
    }



}
